const { createSign, updateSign, getSign } = require("../services/SignService");
const { updateUserIntegral } = require('../services/UserService')
const moment = require('moment')

function getIntegral(value) {
  if (value == 0) {
    return 1
  } else if (value == 1 || value == 2) {
    return 2
  } else if (value === 3 || value == 4) {
    return 4
  } else if (value == 5) {
    return 5
  }
  return Math.ceil((Math.random() * 4 + 6))
}

class SignController {

  // 创建用户头像
  async sign (ctx) {
    const {userId} = ctx.request.body;
    const count = await getSign(userId)
    const now = moment()
    const today = moment(moment().format('YYYY-MM-DD'))
    const yesterday = moment(moment().add(-1, 'd').format('YYYY-MM-DD'))
    if (count.length) {
      const data = count[0].dataValues
      const lastsign = moment(data.lastsign)
      if (lastsign >= today) {
        ctx.body = {
          code: 10001,
          msg: '今日已签到',
        }
      } else if (lastsign >= yesterday) {
        const result = await updateSign({lastsign: now, continuous: data.continuous + 1}, userId)
        const integral = getIntegral(data.continuous)
        await updateUserIntegral(integral, userId)
        ctx.body = {
          code: 200,
          msg: '签到成功',
          data: {
            continuous: data.continuous + 1,
            signed: true,
            integral
          }
        }
      } else {
        const result = await updateSign({lastsign: now, continuous: 1}, userId)
        await updateUserIntegral(1, userId)
        ctx.body = {
          code: 200,
          msg: '签到成功',
          data: {
            continuous: 1,
            signed: true,
            integral: 1
          }
        }
      }
    } else {
      const result = await createSign({userid: userId, lastsign: now, continuous: 1})
      await updateUserIntegral(1, userId)
      ctx.body = {
        code: 200,
        msg: '签到成功',
        data: {
          continuous: 1,
          signed: true,
          integral: 1
        }
      }
    }
  }

  async getSign (ctx) {
    const {userId} = ctx.request.query;
    const count = await getSign(userId)
    const today = moment(moment().format('YYYY-MM-DD'))
    const yesterday = moment(moment().add(-1, 'd').format('YYYY-MM-DD'))
    if (count.length) {
      const data = count[0].dataValues
      const lastsign = moment(data.lastsign)
      if (lastsign >= today) {
        ctx.body = {
          code: 200,
          msg: '今日已签到',
          data: {
            continuous: data.continuous,
            signed: true
          }
        }
      }  else if (lastsign >= yesterday) {
        ctx.body = {
          code: 200,
          msg: '今日未签到',
          data: {
            continuous: data.continuous,
            signed: false
          }
        }
      } else {
        const result = await updateSign({continuous: 0}, userId)
        ctx.body = {
          code: 200,
          msg: '今日未签到',
          data: {
            continuous: 0,
            signed: false
          }
        }
      }
    } else {
      ctx.body = {
        code: 200,
        msg: '查询成功',
        data: {
          continuous: 0,
          signed: false
        }
      }
    }
  }
}

module.exports = new SignController()